﻿@using Model;
@{
    List<QOption> optionList = new List<QOption>();

    Layout = "~/Admin/_AdminSiteLayout.cshtml";
    Page.Title = "Add Question";

    // Validation
    var isValid = true;
    var statusMessage = "";
        // var captchaMessage = "";

    <link href="@Href("~/Styles/Validation.css")" rel="stylesheet" />

    Validation.RequireField("Question", "You must specify a Question.");
    Validation.RequireField("OthersQType", "You must specify a Question Type.");
    Validation.RequireField("readingClass", "You must specify your class.");
    Validation.Add("readingClass", Validator.Range(Question.MinClass, Question.MaxClass, "Class between " + Question.MinClass + " to " + Question.MaxClass));
    Validation.Add("totalAnsweringTime", Validator.Range(Question.MinTime, Question.MaxTime, "Second between " + Question.MinTime + " to " + Question.MaxTime));

    Question aQuestion = null;

    if (!IsPost)
    {
        aQuestion = new Question();
    }

    int i = 1;

    for (; i <= WebSite.OptionPerQuestion; i++)
    {
        Validation.RequireField("Option" + i, "You must specify this option or Remove.");
    }

    i = 0;

    if (!IsPost)
    {
        for (; i < WebSite.OptionPerQuestion; i++)
        {
            QOption aOption = new QOption();

            aOption.Options = "";

            optionList.Add(aOption);
        }
    }

    if (IsPost)
    {
        try
        {
            int isMinAnswer = 0;

            optionList.Clear();

            aQuestion = new Question();

            aQuestion.Questions = Request.Form["Question"];
            aQuestion.QTId = Request.Form["DropDownListType"].AsInt();
            aQuestion.TotalAnsweringTime = Request.Form["totalAnsweringTime"].AsInt();

            if (aQuestion.TotalAnsweringTime == 0)
            {
                aQuestion.TotalAnsweringTime = Question.MaxTime;
            }

            aQuestion.ReadingClass = Request.Form["readingClass"].AsInt();

            aQuestion.CreateDate = DateTime.Now.ToShortDateString();
            aQuestion.IsAnswered = 0;
            aQuestion.AnswerStatus = SiteHelper.GetStatus(aQuestion.IsAnswered);
            aQuestion.Results = 0;

            int questionId = SiteHelper.AddQuestion(aQuestion);

            i = 1;

            string isAnswer = "";

            while (Request.Form["Option" + i] != null)
            {
                QOption aOption = new QOption();

                isAnswer = Request.Form["Option" + i + "CheckBox"];
                aOption.Options = Request.Form["Option" + i];

                if (isAnswer == "on")
                {
                    aOption.IsAnswer = true;
                    isMinAnswer++;
                }
                else
                {
                    aOption.IsAnswer = false;
                }

                aOption.QId = questionId;

                optionList.Add(aOption);

                i++;

                isAnswer = "";
            }
            if (isMinAnswer> 0)
            {
                SiteHelper.AddOptionsList(optionList);

                statusMessage = "Data Saved Successfully!";

                isValid = true;
            }
            else
            {
                statusMessage = "Should minimum 1 answer!";

                isValid = false;
            }
        }
        catch (Exception ex)
        {
            isValid = false;

            statusMessage = "Error!\n" + ex.Message;
        }
    }

    var QuestionTypelist = SiteHelper.GetQTypeListForDropdownList();

}
@* If at least one validation error exists, notify the user *@
@if (!statusMessage.IsEmpty())
{
    if (isValid)
    {
    <p class="message success">
        @statusMessage
    </p>
    }
    else
    {
    <p class="message error">
        @statusMessage
    </p>
    }
}
<form method="post" action="">
<fieldset>
    <legend>Add Question</legend>
    <div class="divwrapper">
        <div class="divwrapper">
            <div class="divwrapper" style="width: 70%; float: left;">
                <div>
                    <label>
                        Question:
                    </label>
                    <textarea id="Question" name="Question" title="Question" rows="3" cols="56" placeholder="Type Question" class="@SiteValidation.GetValidationClass("Question")" @Validation.For("Question")>@aQuestion.Questions </textarea>
                    @Html.ValidationMessage("Question")
                </div>
                <div class="question-area divwrapper">
                    <table id='TextBoxesGroup'>
                        @for (int x = 0; x < optionList.Count; x++)
                        {
                            int increment = x + 1;

                            string idOrName = "Option" + increment;

                            string trId = "TextBoxDiv" + increment;
                            string opCheckBox = "Option" + increment + "CheckBox";

                            <tr id="@trId">
                                <td>
                                    <label>
                                        Option # @increment:
                                    </label>
                                    <input type="text" id="@idOrName" name="@idOrName" title="Question @increment" placeholder="Question @increment" class="@SiteValidation.GetValidationClass(@idOrName)" @Validation.For(@idOrName) value="@optionList[0].Options" />
                                    @Html.ValidationMessage(@idOrName)
                                </td>
                                <td>
                                    <input type="checkbox" id="@opCheckBox" name="@opCheckBox" />
                                    Is Answer?
                                </td>
                            </tr>
                        }
                        @* <tr id="TextBoxDiv1">
                            <td>
                                <label>
                                    Option # 1:
                                </label>
                                <input type="text" id="Option1" name="Option1"  title="Option 1" placeholder="Option 1" class="@SiteValidation.GetValidationClass("Option1")" @Validation.For("Option1") value="@optionList[0].Options" />
                                @Html.ValidationMessage("Option1")
                            </td>
                            <td>
                                <input type="checkbox" id="Option1CheckBox" name="Option1CheckBox" />
                                Is Answer?
                            </td>
                        </tr>
                        <tr id="TextBoxDiv2">
                            <td>
                                <label>
                                    Option # 2:
                                </label>
                                <input type="text" id="Option2" name="Option2"  title="Option 2" placeholder="Option 2" class="@SiteValidation.GetValidationClass("Option2")" @Validation.For("Option2") value="@optionList[1].Options" />
                                @Html.ValidationMessage("Option2")
                            </td>
                            <td>
                                <input type="checkbox" id="Option2CheckBox" name="Option2CheckBox" value="on" />
                                Is Answer?
                            </td>
                        </tr>
                        <tr id="TextBoxDiv3">
                            <td>
                                <label>
                                    Option # 3:
                                </label>
                                <input type="text" id="Option3" name="Option3"  title="Option 3" placeholder="Option 3" class="@SiteValidation.GetValidationClass("Option3")" @Validation.For("Option3") value="@optionList[2].Options" />
                                @Html.ValidationMessage("Option3")
                            </td>
                            <td>
                                <input type="checkbox" id="Option3CheckBox" name="Option3CheckBox" />
                                Is Answer?
                            </td>
                        </tr>
                        <tr id="TextBoxDiv4">
                            <td>
                                <label>
                                    Option # 4:
                                </label>
                                <input type="text" id="Option4" name="Option4"  title="Option 4" placeholder="Option 4" class="@SiteValidation.GetValidationClass("Option4")" @Validation.For("Option4")value="@optionList[3].Options" />
                                @Html.ValidationMessage("Option4")
                            </td>
                            <td>
                                <input type="checkbox" id="Option4CheckBox" name="Option4CheckBox" />
                                Is Answer?
                            </td>
                        </tr>
                        <tr id="TextBoxDiv5">
                            <td>
                                <label>
                                    Option # 5:
                                </label>
                                <input type="text" id="Option5" name="Option5"  title="Option 5" placeholder="Option 5" class="@SiteValidation.GetValidationClass("Option5")" @Validation.For("Option5") value="@optionList[4].Options" />
                                @Html.ValidationMessage("Option5")
                            </td>
                            <td>
                                <input type="checkbox" id="Option5CheckBox" name="Option5CheckBox" />
                                Is Answer?
                            </td>
                        </tr>*@
                    </table>
                    <input class="button thin gray" type='button' value='Add Option' id='addButton' />
                    <input class="button thin gray" type='button' value='Remove Option' id='removeButton' />
                </div>
            </div>
            <div class="divwrapper" style="width: 28%; float: right;">
                <div>
                    <div>
                        <label>
                            Given Time:
                            <br />
                            <span class="shadow-text">( default is @Question.MaxTime second )</span>
                        </label>
                        <input type="text" id="totalAnsweringTime" name="totalAnsweringTime" class="textbox medium" title="Available Time" placeholder="In second" class="@SiteValidation.GetValidationClass("totalAnsweringTime")" @Validation.For("totalAnsweringTime") value="@aQuestion.TotalAnsweringTime" />
                        @Html.ValidationMessage("totalAnsweringTime")
                    </div>
                    <hr />
                    <div>
                        <label>
                            Class:</label>
                        <input type="text" id="readingClass" name="readingClass" class="textbox medium" title="Classs" placeholder="Class" class="@SiteValidation.GetValidationClass("readingClass")" @Validation.For("readingClass") value="@aQuestion.ReadingClass" />
                        @Html.ValidationMessage("readingClass")
                    </div>
                </div>
                <hr />
                <div>
                    <label>
                        Question Types</label>
                    @Html.DropDownList("DropDownListType", QuestionTypelist)
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="divwrapper">
            <p class="form-actions">
                <input type="submit" class="button green medium" value="Save" title="Add Question" />
            </p>
        </div>
    </div>
</fieldset>
<script src="@Href("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery-1.3.2.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var counter = 5;
        $("#addButton").click(function () {
            if (counter>= 5) {
                alert("Maximum 5 Options allow");
                return false;
            }
            counter++;

            var newTextBoxDiv = $(document.createElement('tr')).attr("id", 'TextBoxDiv' + counter);

            newTextBoxDiv.after().html('<td><label>Option #' + counter +
 ' : </label>' + '<input type="text" id="Option ' + counter + '" name="Option ' + counter + '" title="Option ' + counter +
 '" placeholder="Option ' + counter +
 '" class="input-validation-valid" data-val-required="You must specify this option or Remove." data-val="true"><br /><span class="field-validation-valid" data-valmsg-for="Option' + counter +
 '" data-valmsg-replace="true"></span> </td><td><input type="checkbox" id="Option' + counter +
 'CheckBox" name="Option' + counter + 'CheckBox" />Is Answer?</td>');

            newTextBoxDiv.appendTo("#TextBoxesGroup");
        });

        $("#removeButton").click(function () {
            if (counter == 2) {
                alert("Minimum 2 Option must be");
                return false;
            }
            $("#TextBoxDiv" + counter).remove();
            counter--;
        });
    });
</script>
</form>